package com.biaoyoo.www.personapp.core;

import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.SystemClock;

import com.biaoyoo.www.personapp.PersonApp;
import com.biaoyoo.www.personapp.activity.LoginActivity;
import com.biaoyoo.www.personapp.constant.ConstantValue;
import com.biaoyoo.www.personapp.utils.LogUtils;

/**
 * <pre>
 *     author : 40575
 *     e-mail : huyongxi2015@gmail.com
 *     time   : 2017/05/22
 *     desc   : 判断当前app的各个activity的状态
 *     version: 1.0
 * </pre>
 */
public class AppStateTrack implements Application.ActivityLifecycleCallbacks{

    private static final int TIME_VAL = 1 * 60 * 1000 ;
    private static AppStateTrack mAppStateTrack;

    public boolean isLoginActivity() {
        return isLoginActivity;
    }

    /**
     *  ConstantValue.STATE_KILL_BACKGROUD为类初始化的标志变量，表示当前的用户Activity状态
     */
    public int mCurentState = ConstantValue.STATE_KILL_BACKGROUD;

    /**
     * 判断当前应用是否在前台
     */
    private boolean mIsFront = false;

    /**
     * 当前应用在前台的activity数量
     */
    private int mActiceCount = 0;

    private DamonReceiver mReceiver;

    private Context mApp;

    private boolean isLoginActivity ;

    private boolean mIsScrrenOff;
    private long mTimeStamp = 0l;

    private AppStateTrack(PersonApp app){
        this.mApp = app;
        app.registerActivityLifecycleCallbacks(this);
    }

    public static AppStateTrack getInstance(){
        return mAppStateTrack;
    }
    public static void init(PersonApp app){
        mAppStateTrack = new AppStateTrack(app);
    }


    public boolean ismIsFront() {
        return mIsFront;
    }

    public void setScrrenOff(boolean isScrrenOff) {
        this.mIsScrrenOff = isScrrenOff;
    }

    /**
     * 设置当前activity的状态
     * @param mCurentState
     */
    public void setmCurentState(int mCurentState) {
        this.mCurentState = mCurentState;
        if(mCurentState == ConstantValue.STATE_ONLINE){
            if(mReceiver == null){
                IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
                mReceiver = new DamonReceiver();
                mApp.registerReceiver(mReceiver,filter);
            }

        }else if(mReceiver !=null){
            mApp.unregisterReceiver(mReceiver);
            mReceiver = null;
        }
    }


    public boolean checkIfShowGesture(){
        if(mCurentState == ConstantValue.STATE_ONLINE){
            if(mIsScrrenOff){
                return true;
            }
            if(mTimeStamp!=0l && (SystemClock.currentThreadTimeMillis() - mTimeStamp > TIME_VAL)){
                return true;
            }
        }

        return false;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        LogUtils.d(activity.toString()+"---------------onActivityCreated------");
        if(activity instanceof LoginActivity){
            isLoginActivity = true;
        }else{
            isLoginActivity = false;
        }
    }

    @Override
    public void onActivityStarted(Activity activity) {
        LogUtils.d(activity.toString()+"---------------onActivityStarted------");
    }

    @Override
    public void onActivityResumed(Activity activity) {
        LogUtils.d(activity.toString()+"---------------onActivityResumed------");
        mIsFront = true;
        mActiceCount++;
        mIsScrrenOff = false;
    }

    @Override
    public void onActivityPaused(Activity activity) {
        LogUtils.d(activity.toString()+"---------------onActivityPaused------");
    }

    @Override
    public void onActivityStopped(Activity activity) {
        LogUtils.d(activity.toString()+"---------------onActivityStopped------");
        mActiceCount--;
        if(mActiceCount == 0){
            mIsFront = false;
            mTimeStamp = SystemClock.currentThreadTimeMillis();
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        LogUtils.d(activity.toString()+"---------------onActivitySaveInstanceState------");
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        LogUtils.d(activity.toString()+"---------------onActivityDestroyed------");
    }

    class DamonReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(Intent.ACTION_SCREEN_OFF.equals(action)){
                AppStateTrack.getInstance().setScrrenOff(true);
            }

        }
    }
}
